<?php

namespace app\model;

use think\facade\Db;

class NetworkBussinessModel
{
    public static function listsInfo($param)
    {
        $userIds = getScopeView($param['uid']);
        $wheres = ['status', '=', 1];
        if ($userIds != 0) {
            $wheres[] = ['uid', 'in', $userIds];
        }
        if ($param['is_export'] == 1) {
            $param['limit'] = Db::table('xl_networks_business')->where([$wheres])->count();
        }
        $networks_business = Db::table('xl_networks_business')
            ->where([$wheres])
            ->where("partner_id", '=', $param['partner_id'])
            ->limit(($param['page'] - 1) * $param['limit'], $param['limit'])
            ->select()->toArray();
        foreach ($networks_business as $k => $v) {
            $where[] = ['business_id', 'find in set', $v['id']];
            $where[] = ['status', '<', 3];
            $networks_business[$k]['network_count'] = Db::table('xl_networks')->where([$where])->count();
            $networks_business[$k]['time_requirements'] = Db::table('xl_networks_business_time')->where('status', '=', 1)->where("business_id", "=", $v['id'])
                ->field('id,start_time,end_time')->select()->toArray();
        }
        $count = Db::table('xl_networks_business')
            ->where([$wheres])
            ->count();
        return ['networks_business' => $networks_business, 'count' => $count];
    }

    /**
     * 获取所有仓库类型
     * @return void
     */
    public static function GetAllWarehouseType()
    {
        return Db::table('xl_warehouse_type')->where("status", "=", 1)
            ->select();
    }

    /**
     * 获取所有业务类型
     * @return void
     */
    public static function GetAllBussineType()
    {
        return Db::table('xl_business_type')->where("status", "=", 1)
            ->select();
    }

    /**
     * 新增车辆信息
     * @return void
     */
    public static function add($data)
    {
        return Db::table('xl_networks_business')->insert($data);
    }

    /**
     * 新增网点业务信息
     * @return void
     */
    public static function addId($data): int
    {
        return Db::table('xl_networks_business')->insertGetId($data);
    }

    /**
     * 新增网点业务时间信息
     * @return void
     */
    public static function addTime($data): int
    {
        return Db::table('xl_networks_business_time')->insert($data);
    }

    /**
     * 修改网点业务时间信息状态
     * @return int
     */
    public static function editTimeStatus($id)
    {
        return Db::table('xl_networks_business_time')->where("business_id", "=", $id)->update(['status' => 2]);
    }

    /**
     * 修改网点业务时间信息
     * @return void
     */
    public static function editTime($data, $id): int
    {
        return Db::table('xl_networks_business_time')->where("id", "=", $id)->update($data);
    }

    /**
     * 编辑车辆信息
     * @return int
     */
    public static function edit($data, $id)
    {
        return Db::table('xl_networks_business')->where('id', '=', $id)->update($data);
    }

    /**
     * 删除网点业务信息
     * @return int
     */
    public static function del($id)
    {
        return Db::table('xl_networks_business')->where('id', '=', $id)->update(['status' => 3]);
    }

    /**
     * 获取网点业务时间信息
     * @return int
     */
    public static function getNetworksTime($business_id): array
    {
        return Db::table('xl_networks_business_time')->where('business_id', '=', $business_id)->field('start_time,end_time')->select()->toArray();
    }
}
